home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / g__~1 / gplibs20.zoo / sbufvsca.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-13  |  17.0 KB  |  747 lines

  1. /*
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. // Extensively hacked for GNU iostream by Per Bothner 1991, 1992.
  19. // Changes copyright Per Bothner 1992.
  20.  
  21. #if defined(LIBC_SCCS) && !defined(lint)
  22. static char sccsid[] = "%W% (Berkeley) %G%";
  23. #endif /* LIBC_SCCS and not lint */
  24.  
  25. #include <ioprivat.h>
  26. #include <ctype.h>
  27. #ifndef NO_STDARG
  28. #include <stdarg.h>
  29. #else
  30. #include <varargs.h>
  31. #endif
  32.  
  33. #ifndef    NO_FLOATING_POINT
  34. #define FLOATING_POINT
  35. #endif
  36.  
  37. #ifdef FLOATING_POINT
  38. #include <floatio.h>
  39. #define    BUF    (MAXEXP+MAXFRACT+3)    /* 3 = sign + decimal point + NUL */
  40. #else
  41. #define    BUF    40
  42. #endif
  43.  
  44. /*
  45.  * Flags used during conversion.
  46.  */
  47. #define    LONG        0x01    /* l: long or double */
  48. #define    LONGDBL        0x02    /* L: long double; unimplemented */
  49. #define    SHORT        0x04    /* h: short */
  50. #define    SUPPRESS    0x08    /* suppress assignment */
  51. #define    POINTER        0x10    /* weird %p pointer (`fake hex') */
  52. #define    NOSKIP        0x20    /* do not skip blanks */
  53.  
  54. /*
  55.  * The following are used in numeric conversions only:
  56.  * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
  57.  * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
  58.  */
  59. #define    SIGNOK        0x40    /* +/- is (still) legal */
  60. #define    NDIGITS        0x80    /* no digits detected */
  61.  
  62. #define    DPTOK        0x100    /* (float) decimal point is still legal */
  63. #define    EXPOK        0x200    /* (float) exponent (e+3, etc) still legal */
  64.  
  65. #define    PFXOK        0x100    /* 0x prefix is (still) legal */
  66. #define    NZDIGITS    0x200    /* no zero digits detected */
  67.  
  68. /*
  69.  * Conversion types.
  70.  */
  71. #define    CT_CHAR        0    /* %c conversion */
  72. #define    CT_CCL        1    /* %[...] conversion */
  73. #define    CT_STRING    2    /* %s conversion */
  74. #define    CT_INT        3    /* integer, i.e., strtol or strtoul */
  75. #define    CT_FLOAT    4    /* floating, i.e., strtod */
  76.  
  77. #define u_char unsigned char
  78. #define u_long unsigned long
  79.  
  80. extern "C" u_long strtoul(const char*, char**, int);
  81. static const u_char *__sccl(register char *tab, register const u_char *fmt);
  82.  
  83. // If state is non-NULL, set failbit and/or eofbit as appropriate.
  84.  
  85. int streambuf::vscan(char const *fmt0,
  86.              _G_va_list ap,
  87.              ios *stream /* = NULL */)
  88. {
  89.     register const u_char *fmt = (const u_char *)fmt0;
  90.     register int c;        /* character from format, or conversion */
  91.     register size_t width;    /* field width, or 0 */
  92.     register char *p;    /* points into all kinds of strings */
  93.     register int n;        /* handy integer */
  94.     register int flags;    /* flags as defined above */
  95.     register char *p0;    /* saves original value of p when necessary */
  96.     int nassigned;        /* number of fields assigned */
  97.     int nread;        /* number of characters consumed from fp */
  98.     // Assignments to base and ccfn are just to suppress warnings from gcc.
  99.     int base = 0;        /* base argument to strtol/strtoul */
  100.     typedef u_long (*strtoulfn)(const char*, char**, int);
  101.     strtoulfn ccfn = 0;
  102.     // conversion function (strtol/strtoul)
  103.     char ccltab[256];    /* character class table for %[...] */
  104.     char buf[BUF];        /* buffer for numeric conversions */
  105.     int seen_eof = 0;
  106.  
  107.     /* `basefix' is used to avoid `if' tests in the integer scanner */
  108.     static short basefix[17] =
  109.         { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
  110.  
  111.     nassigned = 0;
  112.     nread = 0;
  113.     for (;;) {
  114.         c = *fmt++;
  115.         if (c == 0)
  116.             goto done;
  117.         if (isspace(c)) {
  118.             for (;;) {
  119.                     c = sbumpc();
  120.                 if (c == EOF)
  121.                     goto eof_failure;
  122.                 if (!isspace(c)) {
  123.                     sputbackc(c);
  124.                     break;
  125.                 }
  126.                 nread++;
  127.             }
  128.             continue;
  129.         }
  130.         if (c != '%')
  131.             goto literal;
  132.         width = 0;
  133.         flags = 0;
  134.         /*
  135.          * switch on the format.  continue if done;
  136.          * break once format type is derived.
  137.          */
  138. again:        c = *fmt++;
  139.         switch (c) {
  140.         case '%':
  141. literal:
  142.                 n = sbumpc();
  143.             if (n == EOF)
  144.                 goto eof_failure;
  145.             if (n != c) {
  146.                 sputbackc(n);
  147.                 goto match_failure;
  148.             }
  149.             nread++;
  150.             continue;
  151.  
  152.         case '*':
  153.             flags |= SUPPRESS;
  154.             goto again;
  155.         case 'l':
  156.             flags |= LONG;
  157.             goto again;
  158.         case 'L':
  159.             flags |= LONGDBL;
  160.             goto again;
  161.         case 'h':
  162.             flags |= SHORT;
  163.             goto again;
  164.  
  165.         case '0': case '1': case '2': case '3': case '4':
  166.         case '5': case '6': case '7': case '8': case '9':
  167.             width = width * 10 + c - '0';
  168.             goto again;
  169.  
  170.         /*
  171.          * Conversions.
  172.          * Those marked `compat' are for 4.[123]BSD compatibility.
  173.          *
  174.          * (According to ANSI, E and X formats are supposed
  175.          * to the same as e and x.  Sorry about that.)
  176.          */
  177.         case 'D':    /* compat */
  178.             flags |= LONG;
  179.             /* FALLTHROUGH */
  180.         case 'd':
  181.             c = CT_INT;
  182.             ccfn = (strtoulfn)strtol;
  183.             base = 10;
  184.             break;
  185.  
  186.         case 'i':
  187.             c = CT_INT;
  188.             ccfn = (strtoulfn)strtol;
  189.             base = 0;
  190.             break;
  191.  
  192.         case 'O':    /* compat */
  193.             flags |= LONG;
  194.             /* FALLTHROUGH */
  195.         case 'o':
  196.             c = CT_INT;
  197.             ccfn = strtoul;
  198.             base = 8;
  199.             break;
  200.  
  201.         case 'u':
  202.             c = CT_INT;
  203.             ccfn = strtoul;
  204.             base = 10;
  205.             break;
  206.  
  207.         case 'X':
  208.         case 'x':
  209.             flags |= PFXOK;    /* enable 0x prefixing */
  210.             c = CT_INT;
  211.             ccfn = strtoul;
  212.             base = 16;
  213.             break;
  214.  
  215. #ifdef FLOATING_POINT
  216.         case 'E': case 'F':
  217.         case 'e': case 'f': case 'g':
  218.             c = CT_FLOAT;
  219.             break;
  220. #endif
  221.  
  222.         case 's':
  223.             c = CT_STRING;
  224.             break;
  225.  
  226.         case '[':
  227.             fmt = __sccl(ccltab, fmt);
  228.             flags |= NOSKIP;
  229.             c = CT_CCL;
  230.             break;
  231.  
  232.         case 'c':
  233.             flags |= NOSKIP;
  234.             c = CT_CHAR;
  235.             break;
  236.  
  237.         case 'p':    /* pointer format is like hex */
  238.             flags |= POINTER | PFXOK;
  239.             c = CT_INT;
  240.             ccfn = strtoul;
  241.             base = 16;
  242.             break;
  243.  
  244.         case 'n':
  245.             if (flags & SUPPRESS)    /* ??? */
  246.                 continue;
  247.             if (flags & SHORT)
  248.                 *va_arg(ap, short *) = nread;
  249.             else if (flags & LONG)
  250.                 *va_arg(ap, long *) = nread;
  251.             else
  252.                 *va_arg(ap, int *) = nread;
  253.             continue;
  254.  
  255.         /*
  256.          * Disgusting backwards compatibility hacks.    XXX
  257.          */
  258.         case '\0':    /* compat */
  259.                 nassigned = EOF;
  260.             goto done;
  261.  
  262.         default:    /* compat */
  263.             if (isupper(c))
  264.                 flags |= LONG;
  265.             c = CT_INT;
  266.             ccfn = (strtoulfn)strtol;
  267.             base = 10;
  268.             break;
  269.         }
  270.  
  271.         /*
  272.          * We have a conversion that requires input.
  273.          */
  274.         if (sgetc() == EOF)
  275.             goto eof_failure;
  276.  
  277.         /*
  278.          * Consume leading white space, except for formats
  279.          * that suppress this.
  280.          */
  281.         if ((flags & NOSKIP) == 0) {
  282.             n = (unsigned char)*_gptr;
  283.             while (isspace(n)) {
  284.             _gptr++;
  285.             nread++;
  286.             n = sgetc();
  287.             if (n == EOF)
  288.                 goto eof_failure;
  289.             }
  290.             // Note that there is at least one character in
  291.             // the buffer, so conversions that do not set NOSKIP
  292.             // can no longer result in an input failure.
  293.         }
  294.  
  295.         /*
  296.          * Do the conversion.
  297.          */
  298.         switch (c) {
  299.  
  300.         case CT_CHAR:
  301.             /* scan arbitrary characters (sets NOSKIP) */
  302.             if (width == 0) // FIXME!
  303.                 width = 1;
  304.             if (flags & SUPPRESS) {
  305.                 size_t sum = 0;
  306.                 for (;;) {
  307.                 if ((n = _egptr - _gptr) < (int)width) {
  308.                     sum += n;
  309.                     width -= n;
  310.                     _gptr += n;
  311.                     if (underflow() == EOF)
  312.                     if (sum == 0)
  313.                         goto eof_failure;
  314.                     else {
  315.                         seen_eof++;
  316.                         break;
  317.                     }
  318.                 } else {
  319.                     sum += width;
  320.                     _gptr += width;
  321.                     break;
  322.                 }
  323.                 }
  324.                 nread += sum;
  325.             } else {
  326.                 size_t r = sgetn((char*)va_arg(ap, char*),
  327.                          width);
  328.                 if (r != width)
  329.                 goto eof_failure;
  330.                 nread += r;
  331.                 nassigned++;
  332.             }
  333.             break;
  334.  
  335.         case CT_CCL:
  336.             /* scan a (nonempty) character class (sets NOSKIP) */
  337.             if (width == 0)
  338.                 width = ~0;    /* `infinity' */
  339.             /* take only those things in the class */
  340.             if (flags & SUPPRESS) {
  341.                 n = 0;
  342.                 while (ccltab[(unsigned char)*_gptr]) {
  343.                     n++, _gptr++;
  344.                     if (--width == 0)
  345.                     break;
  346.                     if (sgetc() == EOF) {
  347.                     if (n == 0)
  348.                         goto eof_failure;
  349.                     seen_eof++;
  350.                     break;
  351.                     }
  352.                 }
  353.                 if (n == 0)
  354.                     goto match_failure;
  355.             } else {
  356.                 p0 = p = va_arg(ap, char *);
  357.                 while (ccltab[(unsigned char)*_gptr]) {
  358.                 *p++ = *_gptr++;
  359.                 if (--width == 0)
  360.                     break;
  361.                 if (sgetc() == EOF) {
  362.                     if (p == p0)
  363.                     goto eof_failure;
  364.                     seen_eof++;
  365.                     break;
  366.                 }
  367.                 }
  368.                 n = p - p0;
  369.                 if (n == 0)
  370.                 goto match_failure;
  371.                 *p = 0;
  372.                 nassigned++;
  373.             }
  374.             nread += n;
  375.             break;
  376.  
  377.         case CT_STRING:
  378.             /* like CCL, but zero-length string OK, & no NOSKIP */
  379.             if (width == 0)
  380.                 width = ~0;
  381.             if (flags & SUPPRESS) {
  382.                 n = 0;
  383.                 while (!isspace((unsigned char)*_gptr)) {
  384.                     n++, _gptr++;
  385.                     if (--width == 0)
  386.                         break;
  387.                     if (sgetc() == EOF) {
  388.                         seen_eof++;
  389.                         break;
  390.                     }
  391.                 }
  392.                 nread += n;
  393.             } else {
  394.                 p0 = p = va_arg(ap, char *);
  395.                 while (!isspace((unsigned char)*_gptr)) {
  396.                     *p++ = *_gptr++;
  397.                     if (--width == 0)
  398.                         break;
  399.                     if (sgetc() == EOF) {
  400.                         seen_eof++;
  401.                         break;
  402.                     }
  403.                 }
  404.                 *p = 0;
  405.                 nread += p - p0;
  406.                 nassigned++;
  407.             }
  408.             continue;
  409.  
  410.         case CT_INT:
  411.             /* scan an integer as if by strtol/strtoul */
  412.             if (width == 0 || width > sizeof(buf) - 1)
  413.                 width = sizeof(buf) - 1;
  414.             flags |= SIGNOK | NDIGITS | NZDIGITS;
  415.             for (p = buf; width; width--) {
  416.                 c = (unsigned char)*_gptr;
  417.                 /*
  418.                  * Switch on the character; `goto ok'
  419.                  * if we accept it as a part of number.
  420.                  */
  421.                 switch (c) {
  422.  
  423.                 /*
  424.                  * The digit 0 is always legal, but is
  425.                  * special.  For %i conversions, if no
  426.                  * digits (zero or nonzero) have been
  427.                  * scanned (only signs), we will have
  428.                  * base==0.  In that case, we should set
  429.                  * it to 8 and enable 0x prefixing.
  430.                  * Also, if we have not scanned zero digits
  431.                  * before this, do not turn off prefixing
  432.                  * (someone else will turn it off if we
  433.                  * have scanned any nonzero digits).
  434.                  */
  435.                 case '0':
  436.                     if (base == 0) {
  437.                         base = 8;
  438.                         flags |= PFXOK;
  439.                     }
  440.                     if (flags & NZDIGITS)
  441.                         flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
  442.                     else
  443.                         flags &= ~(SIGNOK|PFXOK|NDIGITS);
  444.                     goto ok;
  445.  
  446.                 /* 1 through 7 always legal */
  447.                 case '1': case '2': case '3':
  448.                 case '4': case '5': case '6': case '7':
  449.                     base = basefix[base];
  450.                     flags &= ~(SIGNOK | PFXOK | NDIGITS);
  451.                     goto ok;
  452.  
  453.                 /* digits 8 and 9 ok iff decimal or hex */
  454.                 case '8': case '9':
  455.                     base = basefix[base];
  456.                     if (base <= 8)
  457.                         break;    /* not legal here */
  458.                     flags &= ~(SIGNOK | PFXOK | NDIGITS);
  459.                     goto ok;
  460.  
  461.                 /* letters ok iff hex */
  462.                 case 'A': case 'B': case 'C':
  463.                 case 'D': case 'E': case 'F':
  464.                 case 'a': case 'b': case 'c':
  465.                 case 'd': case 'e': case 'f':
  466.                     /* no need to fix base here */
  467.                     if (base <= 10)
  468.                         break;    /* not legal here */
  469.                     flags &= ~(SIGNOK | PFXOK | NDIGITS);
  470.                     goto ok;
  471.  
  472.                 /* sign ok only as first character */
  473.                 case '+': case '-':
  474.                     if (flags & SIGNOK) {
  475.                         flags &= ~SIGNOK;
  476.                         goto ok;
  477.                     }
  478.                     break;
  479.  
  480.                 /* x ok iff flag still set & 2nd char */
  481.                 case 'x': case 'X':
  482.                     if (flags & PFXOK && p == buf + 1) {
  483.                         base = 16;    /* if %i */
  484.                         flags &= ~PFXOK;
  485.                         goto ok;
  486.                     }
  487.                     break;
  488.                 }
  489.  
  490.                 /*
  491.                  * If we got here, c is not a legal character
  492.                  * for a number.  Stop accumulating digits.
  493.                  */
  494.                 break;
  495.         ok:
  496.                 /*
  497.                  * c is legal: store it and look at the next.
  498.                  */
  499.                 *p++ = c;
  500.                 _gptr++;
  501.                 if (sgetc() == EOF) {
  502.                     seen_eof++;
  503.                     break;        /* EOF */
  504.                 }
  505.                 }
  506.             /*
  507.              * If we had only a sign, it is no good; push
  508.              * back the sign.  If the number ends in `x',
  509.              * it was [sign] '0' 'x', so push back the x
  510.              * and treat it as [sign] '0'.
  511.              */
  512.             if (flags & NDIGITS) {
  513.                 if (p > buf)
  514.                     (void) sputbackc(*(u_char *)--p);
  515.                 goto match_failure;
  516.             }
  517.             c = ((u_char *)p)[-1];
  518.             if (c == 'x' || c == 'X') {
  519.                 --p;
  520.                 (void) sputbackc(c);
  521.             }
  522.             if ((flags & SUPPRESS) == 0) {
  523.                 u_long res;
  524.  
  525.                 *p = 0;
  526.                 res = (*ccfn)(buf, (char **)NULL, base);
  527.                 if (flags & POINTER)
  528.                     *va_arg(ap, void **) = (void *)res;
  529.                 else if (flags & SHORT)
  530.                     *va_arg(ap, short *) = res;
  531.                 else if (flags & LONG)
  532.                     *va_arg(ap, long *) = res;
  533.                 else
  534.                     *va_arg(ap, int *) = res;
  535.                 nassigned++;
  536.             }
  537.             nread += p - buf;
  538.             break;
  539.  
  540. #ifdef FLOATING_POINT
  541.         case CT_FLOAT:
  542.             /* scan a floating point number as if by strtod */
  543.             if (width == 0 || width > sizeof(buf) - 1)
  544.                 width = sizeof(buf) - 1;
  545.             flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
  546.             for (p = buf; width; width--) {
  547.                 c = (unsigned char)*_gptr;
  548.                 /*
  549.                  * This code mimicks the integer conversion
  550.                  * code, but is much simpler.
  551.                  */
  552.                 switch (c) {
  553.  
  554.                 case '0': case '1': case '2': case '3':
  555.                 case '4': case '5': case '6': case '7':
  556.                 case '8': case '9':
  557.                     flags &= ~(SIGNOK | NDIGITS);
  558.                     goto fok;
  559.  
  560.                 case '+': case '-':
  561.                     if (flags & SIGNOK) {
  562.                         flags &= ~SIGNOK;
  563.                         goto fok;
  564.                     }
  565.                     break;
  566.                 case '.':
  567.                     if (flags & DPTOK) {
  568.                         flags &= ~(SIGNOK | DPTOK);
  569.                         goto fok;
  570.                     }
  571.                     break;
  572.                 case 'e': case 'E':
  573.                     /* no exponent without some digits */
  574.                     if ((flags&(NDIGITS|EXPOK)) == EXPOK) {
  575.                         flags =
  576.                             (flags & ~(EXPOK|DPTOK)) |
  577.                             SIGNOK | NDIGITS;
  578.                         goto fok;
  579.                     }
  580.                     break;
  581.                 }
  582.                 break;
  583.         fok:
  584.                 *p++ = c;
  585.                 _gptr++;
  586.                 if (sgetc() == EOF) {
  587.                     seen_eof++;
  588.                     break;    /* EOF */
  589.                 }
  590.             }
  591.             /*
  592.              * If no digits, might be missing exponent digits
  593.              * (just give back the exponent) or might be missing
  594.              * regular digits, but had sign and/or decimal point.
  595.              */
  596.             if (flags & NDIGITS) {
  597.                 if (flags & EXPOK) {
  598.                     /* no digits at all */
  599.                     while (p > buf)
  600.                         sputbackc(*(u_char *)--p);
  601.                     goto match_failure;
  602.                 }
  603.                 /* just a bad exponent (e and maybe sign) */
  604.                 c = *(u_char *)--p;
  605.                 if (c != 'e' && c != 'E') {
  606.                     (void)sputbackc(c);/* sign */
  607.                     c = *(u_char *)--p;
  608.                 }
  609.                 (void) sputbackc(c);
  610.             }
  611.             if ((flags & SUPPRESS) == 0) {
  612.                 double res;
  613.                 *p = 0;
  614. #ifdef USE_DTOA
  615.                 res = strtod(buf, NULL);
  616. #else
  617.                 res = atof(buf);
  618. #endif
  619.                 if (flags & LONG)
  620.                     *va_arg(ap, double *) = res;
  621.                 else
  622.                     *va_arg(ap, float *) = res;
  623.                 nassigned++;
  624.             }
  625.             nread += p - buf;
  626.             break;
  627. #endif /* FLOATING_POINT */
  628.         }
  629.     }
  630. eof_failure:
  631.     seen_eof++;
  632. input_failure:
  633.     if (nassigned == 0)
  634.         nassigned = -1;
  635. match_failure:
  636.     if (stream)
  637.         stream->set(ios::failbit);
  638. done:
  639.     if (stream && seen_eof)
  640.         stream->set(ios::eofbit);
  641.     return (nassigned);
  642. }
  643.  
  644. /*
  645.  * Fill in the given table from the scanset at the given format
  646.  * (just after `[').  Return a pointer to the character past the
  647.  * closing `]'.  The table has a 1 wherever characters should be
  648.  * considered part of the scanset.
  649.  */
  650. static const u_char *__sccl(register char *tab, register const u_char *fmt)
  651. {
  652.     register int c, n, v;
  653.  
  654.     /* first `clear' the whole table */
  655.     c = *fmt++;        /* first char hat => negated scanset */
  656.     if (c == '^') {
  657.         v = 1;        /* default => accept */
  658.         c = *fmt++;    /* get new first char */
  659.     } else
  660.         v = 0;        /* default => reject */
  661.     /* should probably use memset here */
  662.     for (n = 0; n < 256; n++)
  663.         tab[n] = v;
  664.     if (c == 0)
  665.         return (fmt - 1);/* format ended before closing ] */
  666.  
  667.     /*
  668.      * Now set the entries corresponding to the actual scanset
  669.      * to the opposite of the above.
  670.      *
  671.      * The first character may be ']' (or '-') without being special;
  672.      * the last character may be '-'.
  673.      */
  674.     v = 1 - v;
  675.     for (;;) {
  676.         tab[c] = v;        /* take character c */
  677. doswitch:
  678.         n = *fmt++;        /* and examine the next */
  679.         switch (n) {
  680.  
  681.         case 0:            /* format ended too soon */
  682.             return (fmt - 1);
  683.  
  684.         case '-':
  685.             /*
  686.              * A scanset of the form
  687.              *    [01+-]
  688.              * is defined as `the digit 0, the digit 1,
  689.              * the character +, the character -', but
  690.              * the effect of a scanset such as
  691.              *    [a-zA-Z0-9]
  692.              * is implementation defined.  The V7 Unix
  693.              * scanf treats `a-z' as `the letters a through
  694.              * z', but treats `a-a' as `the letter a, the
  695.              * character -, and the letter a'.
  696.              *
  697.              * For compatibility, the `-' is not considerd
  698.              * to define a range if the character following
  699.              * it is either a close bracket (required by ANSI)
  700.              * or is not numerically greater than the character
  701.              * we just stored in the table (c).
  702.              */
  703.             n = *fmt;
  704.             if (n == ']' || n < c) {
  705.                 c = '-';
  706.                 break;    /* resume the for(;;) */
  707.             }
  708.             fmt++;
  709.             do {        /* fill in the range */
  710.                 tab[++c] = v;
  711.             } while (c < n);
  712. #if 1    /* XXX another disgusting compatibility hack */
  713.             /*
  714.              * Alas, the V7 Unix scanf also treats formats
  715.              * such as [a-c-e] as `the letters a through e'.
  716.              * This too is permitted by the standard....
  717.              */
  718.             goto doswitch;
  719. #else
  720.             c = *fmt++;
  721.             if (c == 0)
  722.                 return (fmt - 1);
  723.             if (c == ']')
  724.                 return (fmt);
  725. #endif
  726.             break;
  727.  
  728.         case ']':        /* end of scanset */
  729.             return (fmt);
  730.  
  731.         default:        /* just another character */
  732.             c = n;
  733.             break;
  734.         }
  735.     }
  736.     /* NOTREACHED */
  737. }
  738.  
  739. int streambuf::scan(char const *format ...)
  740. {
  741.     va_list ap;
  742.     va_start(ap, format);
  743.     int count = vscan(format, ap);
  744.     va_end(ap);
  745.     return count;
  746. }
  747.